package com.hly.musicbackground.model.boots.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hly.musicbackground.Enum.DelEnum;
import com.hly.musicbackground.Final.FictionShelfFinal;
import com.hly.musicbackground.dao.FictionShelfDao;
import com.hly.musicbackground.domain.FictionShelf;
import com.hly.musicbackground.model.boots.VO.FictionShelfVO;
import com.hly.musicbackground.model.boots.service.FictionShelfService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FictionShelfServiceImpl extends ServiceImpl<FictionShelfDao, FictionShelf> implements FictionShelfService {

    @Override
    public void updateNewChapter(int i, int id) {
        baseMapper.updateNewChapter(i, id, FictionShelfFinal.FICTION_SHELF);
    }

    @Override
    public void saveOrUpdateFictionShelfType(int fid, int uid, String type, int fcId, int cNum) {
        FictionShelf fictionShelf = lambdaQuery().eq(FictionShelf::getFictionId, fid)
                .eq(FictionShelf::getUserId, uid)
                .in(FictionShelf::getType, FictionShelfFinal.FICTION_SHELF,
                        FictionShelfFinal.FOOT)
                .one();
        if (fictionShelf == null){
            fictionShelf = new FictionShelf();
            fictionShelf.setFictionId(fid);
            fictionShelf.setUserId(uid);
            fictionShelf.setType(type);
            save(fictionShelf);
        }else {
            if (FictionShelfFinal.FICTION_SHELF.equals(type)){
                fictionShelf.setType(type);
            }
            fictionShelf.setUpdateTime(new Date());
            if (fcId != 0){
                fictionShelf.setChapterNum(cNum);
                fictionShelf.setFictionChapterId(fcId);
            }
            fictionShelf.setNewChapter(0);
            fictionShelf.setDel(DelEnum.NOT_DELETE.getValue());
            updateById(fictionShelf);
        }
    }

    @Override
    public int[] getFictionShelfNumArras(int fid) {
        int[] ints = new int[2];
        ints[1] = getFictionShelfNum(fid, FictionShelfFinal.FICTION_SHELF);
        ints[0] = ints[1] + getFictionShelfNum(fid, FictionShelfFinal.FOOT);
        return ints;
    }

    @Override
    public List<FictionShelfVO> getFictionShelfVOs(int userId, String type, int page, int limit) {
        return baseMapper.getFictionShelfVOs(userId, type, (page - 1) * limit, limit);
    }

    @Override
    public List<Integer> getFictionIds(String type, int uid) {
        return lambdaQuery()
                .select(FictionShelf::getFictionId)
                .eq(FictionShelf::getType, type)
                .eq(uid != 0, FictionShelf::getUserId, uid)
                .list()
                .stream()
                .map(n -> {
                    return n.getFictionId();
                }).collect(Collectors.toList());
    }

    @Override
    public boolean isBookshelf(int uid, int fid, String type) {
        return lambdaQuery().eq(FictionShelf::getUserId, uid)
                .eq(FictionShelf::getFictionId, fid)
                .eq(FictionShelf::getType, type).one() != null;
    }

    @Override
    public void deleteType(String[] fids, int userId, String type) {
        List<FictionShelf> list = lambdaQuery().in(FictionShelf::getFictionId, fids)
                .eq(FictionShelf::getUserId, userId)
                .eq(FictionShelf::getType, type)
                .eq(FictionShelf::getDel, DelEnum.NOT_DELETE.getValue()).list();

        list.stream().forEach(n -> {
            if (n.getType().equals(FictionShelfFinal.FICTION_SHELF)){
                n.setType(FictionShelfFinal.FOOT);
            }else {
                n.setDel(DelEnum.DELETE.getValue());
            }
        });

        updateBatchById(list);
    }

    public int getFictionShelfNum(int fid, String type) {
        LambdaQueryWrapper<FictionShelf> qw = new QueryWrapper<FictionShelf>()
                .select("count(1) as len")
                .lambda()
                .eq(FictionShelf::getFictionId, fid)
                .eq(FictionShelf::getType, type);
        Map<String, Object> map = baseMapper.selectMaps(qw).get(0);
        return Integer.valueOf(String.valueOf(map.get("len")));
    }
}
